<?php

namespace app\server\web\controller;

use app\server\inter\WorkerDriver;
use app\server\model\Config;
use app\server\model\Db;
use Workerman\WebServer;

/**
 * Class Console
 * @package app\server\web\controller
 */
class Console implements WorkerDriver
{

    // 忽略
    const ROOT = "bt.yunfile.com";

    private $worker;

    public function __construct()
    {
        $this->worker = new WebServer(Config::get('web_server.host'));
        $this->worker->addRoot(self::ROOT, SERVER_PATH . 'web' . DS . 'controller' . DS);
        $this->worker->onConnect = [$this, 'onConnect'];
        $this->worker->onWorkerStart = [$this, 'onWorkerStart'];
        $this->worker->onError = [$this, 'onError'];
    }

    public function onConnect($connection)
    {
        $connection->maxPackageSize = Config::get('web_server.max_package_size');
        echo 'connection' . PHP_EOL;
    }

    public function onWorkerStart($worker)
    {
        /**
         * 实例数据库
         */
        if (Config::get('web_server.db')) {
            Db::instance();
        }
        echo 'start' . PHP_EOL;
    }

    public function onMessage($connection, $data)
    {
        // TODO: Implement onMessage() method.
    }

    public function onBufferFull($connection)
    {
        // TODO: Implement onBufferFull() method.
    }

    public function onBufferDrain($connection)
    {
        // TODO: Implement onBufferDrain() method.
    }

    public function onError($connection, $code, $msg)
    {
        echo 'error' . PHP_EOL;
        // TODO: Implement onError() method.
    }

    public function onClose($connection)
    {
        // TODO: Implement onClose() method.
    }
}
